package cn.xiaopengstack.domain.strategy.service.rule.tree.impl;

import cn.xiaopengstack.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import cn.xiaopengstack.domain.strategy.model.valobj.RuleTreeNodeVO;
import cn.xiaopengstack.domain.strategy.service.rule.tree.ILogicTreeNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author jiangyangang
 */
@Component("rule_lock")
@Slf4j
public class RuleLockLogicTreeNode implements ILogicTreeNode {

    @Override
    public DefaultTreeFactory.TreeActionEntity logic(DefaultTreeFactory.TreeActionMatterEntity treeMatter) {

        // 判断用户抽到当前奖品的次数是不是够了
        RuleTreeNodeVO ruleTreeNodeVO = treeMatter.getRuleTreeMap().get("rule_lock");
        String ruleValue = ruleTreeNodeVO.getRuleValue();

        // 查询用户抽到当前奖品的次数
        Integer raffTimes = treeMatter.getUser().getRaffTimes();

        if (Integer.parseInt(ruleValue) == raffTimes) {
            return DefaultTreeFactory.TreeActionEntity.builder()
                    .ruleLogicCheckType(RuleLogicCheckTypeVO.ALLOW)
                    .strategyAwardVo(DefaultTreeFactory.StrategyAwardVO.builder()
                            .awardId(treeMatter.getAwardId())
                            .build()).build();
        } else {
            return DefaultTreeFactory.TreeActionEntity.builder()
                    .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                    .strategyAwardVo(DefaultTreeFactory.StrategyAwardVO.builder()
                            .awardId(treeMatter.getAwardId())
                            .build()).build();
        }
    }
}
